package com.yellowpage.omc.keyword.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.omc.keyword.data.KeywordLog;
import com.yellowpage.omc.keyword.service.KeywordLogService;

public class KeywordLogAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<KeywordLog> keywordLogList = new ArrayList<KeywordLog>();
	private List<Map> resultList = new ArrayList<Map>();

	private KeywordLog keywordLog;
	private KeywordLogService keywordLogService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_keywordLogId;
	private String query_keywordId;
	private String query_createUser;
	private String query_createTime;
	private String query_type;
	private String query_content;
		
	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.keywordLog = keywordLogService.getKeywordLog(Long
					.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("keywordLogId", query_keywordLogId);
		queryMap.put("keywordId", query_keywordId);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("type", query_type);
		queryMap.put("content", query_content);

		resultList = keywordLogService.queryKeywordLogByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		keywordLogService.saveKeywordLog(keywordLog);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
		if (keywordLog.getType().length() == 0) {
			this.addActionError("** 日志类型 不能为空 !");
		}
		if (keywordLog.getContent().length() == 0) {
			this.addActionError("** 日志内容 不能为空 !");
		}

		this.setValidatorResult("/omc/keyword/KeywordLogAdd.jsp");
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    keywordLog = keywordLogService.getKeywordLog(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    keywordLogService.updateKeywordLog(keywordLog);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
		if (keywordLog.getType().length() == 0) {
			this.addActionError("** 日志类型 不能为空 !");
		}
		if (keywordLog.getContent().length() == 0) {
			this.addActionError("** 日志内容 不能为空 !");
		}
		this.setValidatorResult("/omc/keyword/KeywordLogUpdate.jsp");
	}
	
	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.keywordLogService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突,删除失败 !");
		}

		return queryByPage();
	}
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setKeywordLog(KeywordLog keywordLog) {
		this.keywordLog = keywordLog;
	}
	
	public KeywordLog getKeywordLog() {
		return keywordLog;
	}
	 
	 
	 public void setKeywordLogService(KeywordLogService keywordLogService) {
		this.keywordLogService = keywordLogService;
	}
	
	public List<KeywordLog> getKeywordLogList() {
		return keywordLogList;
	}

	public void setKeywordLogList(List<KeywordLog> keywordLogList) {
		this.keywordLogList = keywordLogList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_keywordLogId() {
        return this.query_keywordLogId;
    }
    
    public void setQuery_keywordLogId(String keywordLogId){
        this.query_keywordLogId = keywordLogId;
    }    

        public String getQuery_keywordId() {
        return this.query_keywordId;
    }
    
    public void setQuery_keywordId(String keywordId){
        this.query_keywordId = keywordId;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_type() {
        return this.query_type;
    }
    
    public void setQuery_type(String type){
        this.query_type = type;
    }    

        public String getQuery_content() {
        return this.query_content;
    }
    
    public void setQuery_content(String content){
        this.query_content = content;
    }    

    
	
}
